package com.jsits.v2x.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;

/**
 * 关闭工具类
 * 关闭所有带close方法的对象
 */
public abstract class CloseUtils
{
	/*
	 * 日志
	 */
	private static final Log logger = LogFactory.getLog(CloseUtils.class);

	/*
	 * 关闭结果集
	 */
	public static void close(ResultSet rs)
	{
		if (rs != null)
		{
			try
			{
				rs.close();
			} catch (SQLException e)
			{
				logger.error("close ResultSet failed.", e);
			}
		}
	}

	/*
	 * 关闭执行列表
	 */
	public static void close(Statement stmt)
	{
		if (stmt != null)
		{
			try
			{
				stmt.close();
			} catch (SQLException e)
			{
				logger.error("close Statement failed.", e);
			}
		}
	}

	/*
	 * 关闭PreparedStatement
	 */
	public static void close(PreparedStatement stmt)
	{
		if (stmt != null)
		{
			try
			{
				stmt.close();
			} catch (SQLException e)
			{
				logger.error("close PreparedStatement failed.", e);
			}
		}
	}

	/*
	 * 关闭连接
	 */
	public static void close(Connection connection)
	{
		if (connection != null)
		{
			try
			{
				connection.close();
			} catch (SQLException e)
			{
				logger.error("close Connection failed.", e);
			}
		}
	}

	/*
	 * 关闭套接字连接
	 */
	public static void close(Socket psocket)
	{
		if (psocket != null)
		{
			try
			{
				psocket.close();
			} catch (IOException e)
			{
				logger.error("close socket failed.", e);
			}
		}
	}

	/*
	 * 关闭服务器套接字
	 */
	public static void close(ServerSocket psocket)
	{
		if (psocket != null)
		{
			try
			{
				psocket.close();
			} catch (IOException e)
			{
				logger.error("close socket failed.", e);
			}
		}
	}

	/*
	 * 万能关闭方法
	 * 关闭带close方法的对象
	 */
	public static void close(Object... objs)
	{
		for (Object obj : objs)
		{
			if (obj != null)
			{
				try
				{
					Class<?> objClass = obj.getClass();
					Method meyhod = objClass.getMethod("close", new Class[0]);

					if (null != meyhod)
					{
						meyhod.invoke(obj, new Object[0]);
					} else
					{
						logger.error("This object can not be close, because this object don not have close method.");
					}
				} catch (Exception e)
				{
					logger.error("close Object failed.", e);
				}
			}
		}
	}
}